package com.kbbywyclient.ui.activity;
import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.kbbywyclient.BuildConfig;
import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.app.AppHelper;
import com.kbbywyclient.app.Constants;
import com.kbbywyclient.data.entity.CreditCardIndex;
import com.kbbywyclient.data.entity.LoanIndex;
import com.kbbywyclient.data.entity.SalesSetting;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.MainModule;
import com.kbbywyclient.presenter.MainPresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.contarct.MainContract;
import com.kbbywyclient.util.ActivitySkipUtil;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.ToastUtil;
import com.kbbywyclient.util.ViewUtils;
import com.kbbywyclient.util.download.TDevice;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.BottomPopupOption;
import com.kbbywyclient.widget.CustomDialog;
import com.kbbywyclient.widget.CustomPopwin;
import com.squareup.picasso.Picasso;
import com.umeng.message.common.UmLog;

import java.text.DecimalFormat;
import java.util.HashMap;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 首页
 */
public class MainActivity extends BaseActivity implements MainContract.View {

    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;

    //公共的
    @BindView(R.id.ll_all)
    LinearLayout ll_all;
    @BindView(R.id.iv_head)
    CircleImageView iv_head;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_company)
    TextView tv_company;
    @BindView(R.id.tv_text)
    TextView tv_text;
    @BindView(R.id.tv_business)
    TextView tv_business;
    @BindView(R.id.tv_setting_dot)
    TextView tv_setting_dot;

    private BottomPopupOption bottomPopupOption;//pop弹窗

    private int mType = 0;//0-贷款业务 1-信用卡业务

    private String goToType = "";

    private String cancelActivationDescriptions = "";//不激活说明

    //贷款业务
    @BindView(R.id.ll_loan)
    LinearLayout ll_loan;
    @BindView(R.id.tv_matching_dot)
    TextView tv_matching_dot;
    @BindView(R.id.tv_product_dot)
    TextView tv_product_dot;
    @BindView(R.id.tv_bought_dot)
    TextView tv_bought_dot;
    @BindView(R.id.tv_my_integral)
    TextView tv_my_integral;
    @BindView(R.id.tv_earn_integral)
    TextView tv_earn_integral;
    @BindView(R.id.iv_api_address_divider1)
    ImageView ivApiAddressDivider1;
    @BindView(R.id.rl_switch_api_address1)
    RelativeLayout rlSwitchApiAddress1;
    @BindView(R.id.tv_switch_api_address1)
    TextView tvSwitchApiAddress1;

    //信用卡业务
    @BindView(R.id.ll_credit)
    LinearLayout ll_credit;
    @BindView(R.id.tv_customer_dot_cre)
    TextView tv_customer_dot_cre;
    @BindView(R.id.tv_bought_dot_cre)
    TextView tv_bought_dot_cre;
    @BindView(R.id.tv_my_integral_cre)
    TextView tv_my_integral_cre;
    @BindView(R.id.tv_earn_integral_cre)
    TextView tv_earn_integral_cre;
    @BindView(R.id.iv_api_address_divider2)
    ImageView ivApiAddressDivider2;
    @BindView(R.id.rl_switch_api_address2)
    RelativeLayout rlSwitchApiAddress2;
    @BindView(R.id.tv_switch_api_address2)
    TextView tvSwitchApiAddress2;

    @Inject
    MainPresenter mainPresenter;

    private long mExitTime;

    private String CompanyID = "";
    private String CompanyTypeID = "";

    private int bankCenterDisplay = -1;//

    private CustomPopwin popwin;

    private boolean isDownLoad = false;
    private CustomDialog dialog;
    private int activation = -1;

    private boolean isLoanFrist = true;
    private boolean isCreaditFrist = true;

    private int curVersion = -1;

    AMapLocationClient mLocationClient = null;

    @Override
    public int provideContentViewId() {
        return R.layout.activity_main;
    }

    @OnClick({R.id.ll_setting, R.id.ll_user_info, R.id.tv_business,
            R.id.rl_matching, R.id.rl_product, R.id.rl_bought, R.id.rl_my_integral,
            R.id.rl_earn_integral, R.id.rl_store_manage, R.id.rl_product_manage,
            R.id.rl_my_adviser,
            R.id.rl_switch_api_address1,
            R.id.rl_customer_cre, R.id.rl_bought_cre, R.id.rl_my_integral_cre,
            R.id.rl_earn_integral_cre, R.id.rl_my_adviser_cre, R.id.tv_text,
            R.id.rl_switch_api_address2})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_text:
//                ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
                break;
            case R.id.ll_setting:
                if (PreUtils.isLogin(this)) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("activation", activation);
                    map.put("mType", mType);
                    map.put("curVersion", curVersion);
                    map.put("goToType", goToType);
                    ActivitySkipUtil.skipAnotherActivity(this, SettingActivity.class, map);
                } else {
                    ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
                }
                break;
            case R.id.ll_user_info://基本资料
                if (PreUtils.isLogin(this)) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("activation", activation);
                    ActivitySkipUtil.skipAnotherActivity(this, ModifyInfoActivity.class, map);
                } else {
                    ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
                }
                break;
            case R.id.tv_business://我的业务
                bottomPopupOption.showPopupWindow();
                break;
            case R.id.rl_matching://匹配申请表
                if (PreUtils.isLogin(this)) {
                    if (activation == 1) {//已激活
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("CompanyID", CompanyID);
                        map.put("CompanyTypeID", CompanyTypeID);
                        ActivitySkipUtil.skipAnotherActivity(this, MatchTableActivity.class, map);
                    } else {
                        showToView();
                    }
                } else {
                    ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
                }
                break;
            case R.id.rl_product://产品申请表
                if (PreUtils.isLogin(this)) {
                    if (activation == 1) {//
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("CompanyID", CompanyID);
                        map.put("CompanyTypeID", CompanyTypeID);
                        ActivitySkipUtil.skipAnotherActivity(this, ProductTableActivity.class, map);
                    } else {
                        showToView();
                    }
                } else {
                    ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
                }

                break;
            case R.id.rl_bought://已购买客户

                if (PreUtils.isLogin(this)) {
                    if (activation == 1) {
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("CompanyID", CompanyID);
                        map.put("CompanyTypeID", CompanyTypeID);
                        ActivitySkipUtil.skipAnotherActivity(this, BoughtTableActivity.class, map);
                    } else {
                        showToView();
                    }
                } else {
                    ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
                }
                break;
            case R.id.rl_my_integral://积分
                if (PreUtils.isLogin(this)) {
                    toMineScoreActivity();
                } else {
                    ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
                }
                break;
            case R.id.rl_earn_integral://推荐
                if (PreUtils.isLogin(this)) {
                    ActivitySkipUtil.skipAnotherActivity(this, RecommendScoreActivity.class);
                } else {
                    ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
                }
                break;
            case R.id.rl_store_manage://店铺管理
                if (PreUtils.isLogin(this)) {
                    ActivitySkipUtil.skipAnotherActivity(this, StoreSettingActivity.class);
                } else {
                    ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
                }
                break;
            case R.id.rl_product_manage://产品管理
                if (PreUtils.isLogin(this)) {
                    ActivitySkipUtil.skipAnotherActivity(this, ProductManageActivity.class);
                } else {
                    ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
                }
                break;
            case R.id.rl_my_adviser://我的顾问
                if (PreUtils.isLogin(this)) {
                    ActivitySkipUtil.skipAnotherActivity(this, MyBusinessConActivity.class);
                } else {
                    ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
                }
                break;
            case R.id.rl_customer_cre://客户申请表
                if (PreUtils.isLogin(this)) {
                    if (activation == 1) {
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("CompanyID", CompanyID);
                        map.put("CompanyTypeID", CompanyTypeID);
                        ActivitySkipUtil.skipAnotherActivity(this, CustomerTableActivity.class, map);
                    } else {
                        showToView();
                    }
                } else {
                    ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
                }
                break;
            case R.id.rl_bought_cre://已购买客户 信用卡
                if (PreUtils.isLogin(this)) {
                    if (activation == 1) {
                        ActivitySkipUtil.skipAnotherActivity(this, BoughtTableCreActivity.class);
                    } else {
                        showToView();
                    }
                } else {
                    ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
                }
                break;
            case R.id.rl_my_integral_cre:// 积分
                if (PreUtils.isLogin(this)) {
                    toMineScoreActivity();
                } else {
                    ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
                }
                break;
            case R.id.rl_earn_integral_cre://推荐
                if (PreUtils.isLogin(this)) {
                    ActivitySkipUtil.skipAnotherActivity(this, RecommendScoreActivity.class);
                } else {
                    ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
                }
                break;
            case R.id.rl_my_adviser_cre://我的顾问
                if (PreUtils.isLogin(this)) {
                    ActivitySkipUtil.skipAnotherActivity(this, MyBusinessConActivity.class);
                } else {
                    ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
                }
                break;
            case R.id.rl_switch_api_address1:
                showApiAddressChooser(1);
                break;
            case R.id.rl_switch_api_address2:
                showApiAddressChooser(2);
                break;
        }
    }

    /**
     * 去填写资料
     */
    private void showInfoDialog() {
        dialog = new CustomDialog(this, R.style.Customdialog, "卡贝贝提示", "您未设置基本资料，请先设置基本资料"
                , new CustomDialog.ButtonListener() {
            @Override
            public void left() {
                dialog.dismiss();
            }

            @Override
            public void right() {
                ActivitySkipUtil.skipAnotherActivity(MainActivity.this, ModifyInfoActivity.class);
                dialog.dismiss();
            }
        }, getString(R.string.cancel), "设置基本资料");
        dialog.show();
    }

    /**
     * 去认证
     */
    private void showAuthentDialog() {
        dialog = new CustomDialog(this, R.style.Customdialog, "卡贝贝提示", "您未通过资质认证，请先资质认证"
                , new CustomDialog.ButtonListener() {
            @Override
            public void left() {
                dialog.dismiss();
            }

            @Override
            public void right() {
                HashMap<String, Object> map = new HashMap<>();
                map.put("mType", mType);
                switch (mType) {
                    case 0:
                        ActivitySkipUtil.skipAnotherActivity(MainActivity.this, InfoCertifyLoanActivity.class, map);
                        break;
                    case 1:
                        ActivitySkipUtil.skipAnotherActivity(MainActivity.this, InfoCertifyCreditActivity.class, map);
                        break;
                }
                dialog.dismiss();
            }
        }, getString(R.string.cancel), "资质认证");
        dialog.show();
    }

    /**
     * 资料 审核中
     */
    private void showExamineDialog() {
        dialog = new CustomDialog(this, R.style.Customdialog, "卡贝贝提示", "您的资料审核中..."
                , new CustomDialog.ButtonListener() {
            @Override
            public void left() {
                dialog.dismiss();
            }

            @Override
            public void right() {
                dialog.dismiss();
            }
        }, getString(R.string.cancel), getString(R.string.determine));
        dialog.show();
    }

    /**
     * 提示认证不通过
     */
    private void showReasonDialog() {
        dialog = new CustomDialog(this, R.style.Customdialog, "卡贝贝提示", "认证未通过(" + cancelActivationDescriptions + ")"
                , new CustomDialog.ButtonListener() {
            @Override
            public void left() {
                dialog.dismiss();
            }

            @Override
            public void right() {
                dialog.dismiss();
            }
        }, getString(R.string.cancel), getString(R.string.determine));
        dialog.show();
    }

    /**
     * 提示 去店铺
     */
    private void showStoreDialog() {
        dialog = new CustomDialog(this, R.style.Customdialog, "卡贝贝提示", "建议您先完善店铺信息,匹配客户更精准！"
                , new CustomDialog.ButtonListener() {
            @Override
            public void left() {
                dialog.dismiss();
            }

            @Override
            public void right() {
                ActivitySkipUtil.skipAnotherActivity(MainActivity.this, StoreSettingActivity.class);
                dialog.dismiss();
            }
        }, getString(R.string.cancel), "店铺设置");
        dialog.show();
    }

    /**
     * 我的积分
     */
    private void toMineScoreActivity() {
        if (PreUtils.isLogin(this)) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("CompanyID", CompanyID);
            map.put("CompanyTypeID", CompanyTypeID);
            ActivitySkipUtil.skipAnotherActivity(this, MineScoreActivity.class, map);
        } else {
            ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
        }
    }

    @Override
    public void initView() {
        //高德定位
        initLocation();
        //处理信用卡、贷款业务，默认为贷款业务
        mType=PreUtils.getBusinessId(this);
        if(mType!=1){
            //解决旧版本冲突问题
            PreUtils.setBusinessId(this,0);
        }

        bottomPopupOption = new BottomPopupOption(this);
        bottomPopupOption.setItemText(getString(R.string.text_my_loan), getString(R.string.text_my_credit));
        bottomPopupOption.setItemClickListener(position -> {
            switch (position) {
                case 1://信用卡
                    if (PreUtils.isLogin(this)) {
//                        if (bankCenterDisplay == 0) {//不是总行信用卡
                            setCreditView();
                            mainPresenter.getCreditCardIndex(getUser(this).getSalesID() + "",true);
//                        } else {//总行
//                            ToastUtil.showShort("您所在行暂不支持信用卡经理入驻");
//                        }
                    } else {
                        setCreditView();
                    }
                    break;
                default://贷款
                    setLoanView();
                    if (PreUtils.isLogin(this)) {
                        mainPresenter.getLoanIndex(getUser(this).getSalesID() + "",true);
                    }
                    break;
            }
            PreUtils.setBusinessId(this, mType);
            bottomPopupOption.dismiss();
        });

        refreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorAccent));
        refreshLayout.setOnRefreshListener(() -> {
            switch (PreUtils.getBusinessId(this)) {
                case 1://信用卡
                    setCreditView();
                    if (PreUtils.isLogin(this)) {
                        mainPresenter.getCreditCardIndex(getUser(this).getSalesID() + "",false);
                    }
                    break;
                default://贷款
                    setLoanView();
                    if (PreUtils.isLogin(this)) {
                        mainPresenter.getLoanIndex(getUser(this).getSalesID() + "",false);
                    }
                    break;
            }
        });
    }

    private void setCreditView() {
        setScreenName(getString(R.string.google_credit_view));
        mType = 1;
        tv_business.setText(getString(R.string.text_my_credit));
        ll_loan.setVisibility(View.GONE);
        ll_credit.setVisibility(View.VISIBLE);
    }

    private void setLoanView() {
        setScreenName(getString(R.string.google_loan_view));
        mType = 0;
        tv_business.setText(getString(R.string.text_my_loan));
        ll_loan.setVisibility(View.VISIBLE);
        ll_credit.setVisibility(View.GONE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        final boolean isLogin=PreUtils.isLogin(this);
        if (!isLogin) {
            Picasso.with(this).
                    load(R.mipmap.icon_head_portrait).
                    placeholder(R.mipmap.icon_head_portrait).
                    error(R.mipmap.icon_head_portrait).
                    into(iv_head);
            ViewUtils.setText(tv_name, "未登录");
            ViewUtils.setText(tv_company, "");

            tv_matching_dot.setVisibility(View.INVISIBLE);
            tv_product_dot.setVisibility(View.INVISIBLE);
            tv_customer_dot_cre.setVisibility(View.INVISIBLE);

            switch (mType) {
                case 0:
                    ViewUtils.setText(tv_my_integral, "");
                    ViewUtils.setText(tv_earn_integral, "");
                    break;
                case 1:
                    ViewUtils.setText(tv_my_integral_cre, "");
                    ViewUtils.setText(tv_earn_integral_cre, "");
                    break;
            }

        }

        //非生产环境时显示切换测试地址布局
        if(!Constants.PRODUCT_ENVIRONMENT.equals(BuildConfig.BUILD_TYPE)) {
            switch (PreUtils.getApiAddressType(AppHelper.getApplicationContext())) {
                case 1:
                    tvSwitchApiAddress1.setText("测试环境(21)");
                    tvSwitchApiAddress2.setText("测试环境(21)");
                    break;
                case 2:
                    tvSwitchApiAddress1.setText("测试环境(22)");
                    tvSwitchApiAddress2.setText("测试环境(22)");
                    break;
                case 3:
                    tvSwitchApiAddress1.setText("灰度环境");
                    tvSwitchApiAddress2.setText("灰度环境");
                    break;
                case 4:
                    tvSwitchApiAddress1.setText("生产环境");
                    tvSwitchApiAddress2.setText("生产环境");
                    break;
                default:
                    tvSwitchApiAddress1.setText("开发环境");
                    tvSwitchApiAddress2.setText("开发环境");
                    break;
            }
            ivApiAddressDivider1.setVisibility(View.VISIBLE);
            rlSwitchApiAddress1.setVisibility(View.VISIBLE);

            ivApiAddressDivider2.setVisibility(View.VISIBLE);
            rlSwitchApiAddress2.setVisibility(View.VISIBLE);
        }

        refreshLayout.setEnabled(isLogin);

        if (isDownLoad) {
            showBusinessView();
        } else {
            if (isLogin) {
                mainPresenter.getSalesSetting(PreUtils.getUser(this).getSalesID() + "");
            }
        }
    }

    @Override
    public void initData() {


    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getMainComponent(new MainModule(this)).inject(this);//注入view
    }

    private void initLocation() {
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();

        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //获取一次定位结果
        mLocationOption.setOnceLocation(true);
        //关闭缓存机制
        mLocationOption.setLocationCacheEnable(false);

        //声明定位回调监听器
        AMapLocationListener mLocationListener = aMapLocation -> {
            if (aMapLocation != null) {
                if (aMapLocation.getErrorCode() == 0) {
                    //解析定位结果
                    UmLog.i("aaa", "city: " + aMapLocation.getCity());
                    PreUtils.setCityName(this, aMapLocation.getCity());
                    String city = PreUtils.getUser(this).getCityName();
                    UmLog.i("aaa", "city1: " + city);
                }
            }
        };

        //初始化定位
        mLocationClient = new AMapLocationClient(this);
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();

    }

    /**
     * 更改返回按键的响应事件
     *
     * @param keyCode
     * @param event
     * @return
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                ToastUtil.showShort(getString(R.string.exit_application));
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
                if (mLocationClient != null) {
                    mLocationClient.stopLocation();//停止定位后，本地定位服务并不会被销毁
                    mLocationClient.onDestroy();//销毁定位客户端，同时销毁本地定位服务。
                }
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    @Override
    public void loadSucLoanIndex(LoanIndex loanIndex) {
        refreshLayout.setRefreshing(false);
        if (loanIndex.getCity() != null) {
            if (StringUtil.isNotEmpty(loanIndex.getCity().toString())) {
                PreUtils.setCity(this, Integer.valueOf(new DecimalFormat("0").format(loanIndex.getCity())));
            }
        }

        goToType = loanIndex.getGoToType();
        activation = loanIndex.getActivation();
//        bankCenterDisplay = loanIndex.getBankCenterDisplay();

        cancelActivationDescriptions = loanIndex.getCancelActivationDescriptions();

        CompanyID = loanIndex.getCompanyID() + "";
        CompanyTypeID = loanIndex.getCompanyTypeID() + "";

        PreUtils.setCompanyID(this, CompanyID);
        PreUtils.setCompanyTypeID(this, CompanyTypeID);

        Picasso.with(this).
                load(API.PIC_ADDRESS + loanIndex.getLogo()).
                placeholder(R.mipmap.icon_head_portrait).
                error(R.mipmap.icon_head_portrait).
                into(iv_head);
        if (!TextUtils.isEmpty(loanIndex.getTrueName())) {
            ViewUtils.setText(tv_name, loanIndex.getTrueName());
        } else {
            ViewUtils.setText(tv_name, PreUtils.getUserAccount(this));
        }
        ViewUtils.setText(tv_company, loanIndex.getNameShort());
        if (loanIndex.getNewPPOrderCount() > 0) {
            tv_matching_dot.setVisibility(View.VISIBLE);
            ViewUtils.setText(tv_matching_dot, loanIndex.getNewPPOrderCount() + "");
        } else if (loanIndex.getNewPPOrderCount() > 100) {
            tv_matching_dot.setVisibility(View.VISIBLE);
            ViewUtils.setText(tv_matching_dot, "99");
        } else {
            tv_matching_dot.setVisibility(View.INVISIBLE);
        }
        if (loanIndex.getNewProductOrderCount() > 0) {
            tv_product_dot.setVisibility(View.VISIBLE);
            ViewUtils.setText(tv_product_dot, loanIndex.getNewProductOrderCount() + "");
        } else if (loanIndex.getNewProductOrderCount() > 100) {
            tv_product_dot.setVisibility(View.VISIBLE);
            ViewUtils.setText(tv_product_dot, "99");
        } else {
            tv_product_dot.setVisibility(View.INVISIBLE);
        }

        switch (activation) {
            case 2://待审
            case 3://不通过
            case 4://未填写
            case 0://未激活
                if (isLoanFrist) {
                    isLoanFrist = false;
                    showToView();
                }
                break;
            case 1://激活
                ViewUtils.setText(tv_my_integral, loanIndex.getScore() + "");
                ViewUtils.setText(tv_earn_integral, "累计奖励：" + loanIndex.getRewardScore());
                break;

        }

    }

    /**
     * 业务未激活 的操作
     */
    private void showToView() {
        switch (goToType) {
            case "0"://不跳转
                break;
            case "1"://去填基本资料
                showInfoDialog();
                break;
            case "2"://去认证
                showAuthentDialog();
                break;
            case "3"://提示资料待审中
                showExamineDialog();
                break;
            case "4"://认证未通过提示原因
                showReasonDialog();
                break;
            case "5"://去店铺
//                showStoreDialog();
                showExamineDialog();
                break;
        }
    }

    @Override
    public void loadFaiLoanIndex(String errorMsg) {
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void loadSucCreditIndex(CreditCardIndex creditCardIndex) {
        refreshLayout.setRefreshing(false);
        if (creditCardIndex.getCity() != null) {
            if (StringUtil.isNotEmpty(creditCardIndex.getCity().toString())) {
                PreUtils.setCity(this, Integer.valueOf(new DecimalFormat("0").format(creditCardIndex.getCity())));
            }
        }
//        if (creditCardIndex.getBankCenterDisplay() == 1) {//总行跳到贷款业务
//            setLoanView();
//        }

        goToType = creditCardIndex.getGoToType();
        activation = creditCardIndex.getActivation();
        cancelActivationDescriptions = creditCardIndex.getCancelActivationDescriptions();

        Picasso.with(this).
                load(API.PIC_ADDRESS + creditCardIndex.getLogo()).
                placeholder(R.mipmap.icon_head_portrait).
                error(R.mipmap.icon_head_portrait).
                into(iv_head);

        if (!TextUtils.isEmpty(creditCardIndex.getTrueName())) {
            ViewUtils.setText(tv_name, creditCardIndex.getTrueName());
        } else {
            ViewUtils.setText(tv_name, PreUtils.getUserAccount(this));
        }
        ViewUtils.setText(tv_company, creditCardIndex.getNameShort());

        if (creditCardIndex.getNewOrderCount() > 0) {
            tv_customer_dot_cre.setVisibility(View.VISIBLE);
            ViewUtils.setText(tv_customer_dot_cre, creditCardIndex.getNewOrderCount() + "");
        } else if (creditCardIndex.getNewOrderCount() > 100) {
            tv_customer_dot_cre.setVisibility(View.VISIBLE);
            ViewUtils.setText(tv_customer_dot_cre, "99");
        } else {
            tv_customer_dot_cre.setVisibility(View.INVISIBLE);
        }

        switch (activation) {
            case 2://待审
            case 3://不通过
            case 4://未填写
            case 0://未激活
                if (isCreaditFrist) {
                    isCreaditFrist = false;
                    showToView();
                }
                break;
            case 1://激活
                ViewUtils.setText(tv_my_integral_cre, creditCardIndex.getScore() + "");
                ViewUtils.setText(tv_earn_integral_cre, "累计奖励：" + creditCardIndex.getRewardScore());
                break;
        }

    }

    @Override
    public void loadFaiCreditIndex(String errorMsg) {
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void loadSucSaleSetting(SalesSetting salesSetting) {
        final SalesSetting.SalesSettingBean bean=salesSetting.getSalesSetting();
        curVersion = bean.getAndroidVersion();
        if (TDevice.getVersionCode() < curVersion) {//新版本
            showDialogView(bean.getAndroidUpdate(),
                    bean.getVersion_up_desc(),
                    bean.getAndroidDownloadURL());
        } else {
            showBusinessView();
        }

    }

    private void showBusinessView() {
        switch (PreUtils.getBusinessId(this)) {
            case 1://信用卡
                setCreditView();
                if (PreUtils.isLogin(this)) {
                    mainPresenter.getCreditCardIndex(getUser(this).getSalesID() + "",true);
                }
                break;
            default://贷款
                setLoanView();
                if (PreUtils.isLogin(this)) {
                    mainPresenter.getLoanIndex(getUser(this).getSalesID() + "",true);
                }
                break;
        }
    }

    @Override
    public void loadFaiSaleSetting(String errorMsg) {

    }

    private void showDialogView(final String forceUpdate,String updateDes,final String url) {
        final String updateDesDefault="有新版本发布啦,快来更新体验新功能吧！";
        AlertDialog updateDialog=new AlertDialog.Builder(this).
                setTitle("更新提醒").
                setMessage(TextUtils.isEmpty(updateDes)?updateDesDefault:updateDes).
                setNegativeButton("取消", (dialog, which) -> {
                    isDownLoad=false;
                    if("1".equals(forceUpdate)){
                        finish();
                    }
                }).setPositiveButton("确定", (dialog, which) -> {
                    isDownLoad=true;
                    TDevice.openDownLoadService(MainActivity.this, url, "更新中...");
                    showBusinessView();
                }).setCancelable(false).create();
        updateDialog.show();
    }

    /**
     * 显示切换测试地址对话框
     * @param position 触发的位置 1、贷款业务中切换测试地址触发 2、信用卡业务中切换测试地址触发
     */
    private void showApiAddressChooser(final int position){
        final Context context=MainActivity.this;
        final String[] items=new String[]{"测试环境(21)","测试环境(22)","灰度环境","生产环境"};
        AlertDialog.Builder builder=new AlertDialog.Builder(context);
        builder.setTitle("选择测试环境");
        builder.setItems(items, (dialog, which) -> {
            //如果选择的测试环境与目前所用的测试环境是一样的，不处理
            //否则保存选择的测试环境，提示重启app
            int select=which+1;
            if(PreUtils.getApiAddressType(context)!=select){
                PreUtils.setApiAddressType(context,select);
                //显示测试环境
                if(position==1){
                    tvSwitchApiAddress1.setText(items[which]);
                }else{
                    tvSwitchApiAddress2.setText(items[which]);
                }
                //退出登录状态
                PreUtils.clearUser(context);
                ToastUtil.showLong("切换成功，重新打开App即可生效!");
                finish();
                System.exit(0);//退出
            }
        });
        builder.show();
    }
}
